#include <bits/stdc++.h>
using namespace std;
#define M 200005
#define N 5001
int fa[N];
int n, m, e_cnt, ans;

struct Edge {
	int from, to, w;
	inline friend bool operator<(const Edge &a, const Edge &b) { return a.w < b.w; }
} e[M];

inline void init() {
	for (int i = 1; i <= n; ++i) { fa[i] = i; }
}

int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

int main() {
	cin >> n >> m;
	init();
	int a, b, c;
	for (int i = 1; i <= m; ++i) { cin >> e[i].from >> e[i].to >> e[i].w; }
	sort(e + 1, e + m + 1);
	int cnt = 0;
	for (int i = 1; i <= m; ++i) {
		int fin = find(e[i].from);
		int fout = find(e[i].to);
		if (fin != fout) {
			ans += e[i].w;
			fa[fout] = fin;
			if (++cnt == n - 1) { break; }
		}
	}
	cout << ans;
	return 0;
}